﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace canyinguanlixitong
{
    public partial class frmqw1xinzeng : Form
    {
        public frmqw1xinzeng()
        {
            InitializeComponent();
        }


     
        string str = "Data Source=.;Initial Catalog=Supermarket management system;User ID=sa;Password=123456";
       private void button1_Click(object sender, EventArgs e)
        {
         
                if (yanzheng())
                {
                    ass();
                    this.Close();
                }
        }
        //会员等级下拉列表
       public void add()
       {
           SqlConnection conn = new SqlConnection(str);
         try 
	{	        
		  DataSet ds = new DataSet("Student");
           string sql = "select * from dbo.VIPtype";
           SqlDataAdapter asd = new SqlDataAdapter(sql,conn);
           asd.Fill(ds,"Student");
           this.cbodengji.DataSource = ds.Tables["Student"];
           this.cbodengji.ValueMember="vipLeiHao";
           this.cbodengji.DisplayMember = "vipLeiName";
           DataRow row = ds.Tables["Student"].NewRow();
           row["vipLeiHao"] = -1;
           row["vipLeiName"] = "全部";
           ds.Tables["Student"].Rows.InsertAt(row, 0);
            
	}
	catch (Exception ex)
	{
	MessageBox.Show(ex.Message);
	}

       }
//验证不为空
       public bool yanzheng() { 
       if(this.txtmone.Text.Trim().Equals(string.Empty)){
           this.txtmone.Focus();
           MessageBox.Show("金额不能为空");
           return false;
       } if(this.txtname.Text.Trim().Equals(string.Empty)){
           this.txtname.Focus();
           MessageBox.Show("姓名不能为空");
           return false;
       }if(this.txtphone.Text.Trim().Equals(string.Empty)){
           this.txtphone.Focus();
           MessageBox.Show("联系号码不能为空");
           return false;
       } if (this.txtstu.Text.Trim().Equals(string.Empty))
       {
           this.txtstu.Focus();
           MessageBox.Show("编号不能为空");
           return false;
       }
       else {
           return true;
       }
       
       }
        //新增
       public void ass() {
           SqlConnection conn = new SqlConnection(str);
           try
           {
               conn.Open();
               int bianhao = Convert.ToInt32(this.txtstu.Text);
               string name = this.txtname.Text;
               string sex = "";
               if (radioButton1.Checked)
               {
                   sex = "男";

               }
               else
               {
                   sex = "女";
               }
               double mone = Convert.ToDouble(this.txtmone.Text);
               string phone = this.txtphone.Text;
               int i = Convert.ToInt32(this.cbodengji.SelectedIndex.ToString()); 
               int pass = Convert.ToInt32(this.txtpass.Text);
               string qw = this.textBox1.Text.ToString();
               string sql = "insert into dbo.VIPuser values('" + bianhao + "','" + name + "','" + sex + "','" + mone + "','" + phone + "','" + i + "','" + pass + "','" + qw + "') select @@Identity";
               SqlCommand comm = new SqlCommand(sql, conn);
               int iq = comm.ExecuteNonQuery();
               if (iq > 0)
               {
                   MessageBox.Show("新增成功");
                  
               }
               else
               {
                   MessageBox.Show("新增失败");
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message+"报错");
           }
           finally {
               conn.Close();
           }
       }
    


       private void frmqw1xinzeng_Load(object sender, EventArgs e)
       {
           add();
       this.cbodengji.SelectedIndex = 0;
       }

       private void button2_Click(object sender, EventArgs e)
       {
           this.Close();
       }

     
    
    }
}
